/*
 * @lc app=leetcode.cn id=400 lang=java
 *
 * [400] 第 N 位数字
 */

// @lc code=start
class Solution {
    public int findNthDigit(int n) {
        // 1. find the digit
        int digit = 1;
        int start = 1;
        long count = 9;
        int num = 0;
        while(n > count){
            n -= (int)count;
            start *= 10;
            digit++;
            count = (long)9*start*digit;
        }
        num = start + (n - 1) / digit;
        int index = (n - 1)%digit;
        while(index < digit-1){
            num = num/10;
            digit--;
        }
        return num%10;
    }
}
// @lc code=end

